// 简化版API接口配置 - 兼容性更好
const baseURL = 'http://localhost:8036';

// 导出配置对象
export const config = {
  baseURL: baseURL,
  timeout: 10000
};

// 简单的HTTP请求封装
function request(options) {
  return new Promise(function(resolve, reject) {
    const config = {
      url: baseURL + options.url,
      method: options.method || 'GET',
      data: options.data || {},
      header: {
        'Content-Type': 'application/json',
        'Accept': 'application/json'
      },
      timeout: 10000,
      success: function(response) {
        console.log('API请求成功:', response);
        resolve(response.data);
      },
      fail: function(error) {
        console.error('API请求失败:', error);
        reject(error);
      }
    };
    
    // 添加token
    const token = uni.getStorageSync('token');
    if (token) {
      config.header.Authorization = 'Bearer ' + token;
    }
    
    uni.request(config);
  });
}

// API接口定义
export const API = {
  // Banner相关接口
  banner: {
    getList: function(params) {
      return request({
        url: '/app/api/banner/list',
        method: 'GET',
        data: params
      });
    }
  },

  // 仓库相关接口
  warehouse: {
    getList: function(params) {
      return request({
        url: '/app/api/warehouse/list',
        method: 'GET',
        data: params
      });
    },
    
    getDetail: function(id) {
      return request({
        url: '/app/api/warehouse/' + id,
        method: 'GET'
      });
    }
  },

  // 资讯相关接口
  news: {
    getList: function(params) {
      return request({
        url: '/app/api/news/list',
        method: 'GET',
        data: params
      });
    },
    
    getDetail: function(id) {
      return request({
        url: '/app/api/news/' + id,
        method: 'GET'
      });
    }
  },

  // 首页相关接口
  home: {
    getData: function(params) {
      return request({
        url: '/app/api/home/data',
        method: 'GET',
        data: params
      });
    }
  },

  // 预约相关接口
  appointment: {
    getList: function(params) {
      return request({
        url: '/app/api/appointment/list',
        method: 'GET',
        data: params
      });
    },
    
    create: function(data) {
      return request({
        url: '/app/api/appointment/create',
        method: 'POST',
        data: data
      });
    },
    
    cancel: function(id) {
      return request({
        url: '/app/api/appointment/cancel/' + id,
        method: 'PUT'
      });
    }
  }
}; 